function Learn_Part_edgeFeat3()

%addpath(genpath('../pose-release1.2-full2/pose-release-ver1.2/code-full/'));
% ------------- load train model ----------------------------------
load('mydata', 'part_shape',  'pos', 'pos2');    
sampleNum   =   length(pos);
pos         =   pos2(1:sampleNum/2);
cluster_num  =   6;
grid_size   =   4;

sbin        =   8;
% C           =   0.2;  % svm C parameter in QP
% wpos        =   2;      % svm pos  in QP
% 
% channel_num    =   size(part_shape{2}.edgefeat,3);
% 
% nmax   =    5000;

verbose     =   1;

% clustering.................
load('mydata','exampler_idx');
if(~exist('exampler_idx', 'var'))
    load('mydata','exampler_idx');
    for partID  = [4 ],
        feat_sz          =   size(part_shape{partID}.feat);
        exampler_set     =   reshape(part_shape{partID}.feat, feat_sz(1)*feat_sz(2), feat_sz(3));   
        [gIdx,c,sumdist] =   k_means(double(exampler_set'), cluster_num);  
        exampler_idx(:, partID) = gIdx;
    end

    save('mydata', 'exampler_idx', '-append');
end


 
% show part's member cluster result
if verbose 
    row_num     =   ceil(cluster_num/5);    
    partID      =   2;
    figure(1);
    for c = 1:cluster_num
        vl_tightsubplot(2, 3, c) ;
        inst_id     =   find(exampler_idx(:,partID) == c)';        
        clus_shape  =   part_shape{partID}.feat(:,:,inst_id);
        imagesc(sum(clus_shape,3)./length(inst_id));    
        %axis image off ;          
        %colormap gray ;
        set(gca,'ytick',[])

    end
    vl_demo_print('shape_cluster') ;
    figure(2), hist(exampler_idx(:,partID), [1:cluster_num]);
end    
    
% DPM parameters  7 nodes 
par             =       [0   1 2 3 4   5 6];
K               =       [2   3 3 3 3   2 2];      % cluster Number 
distance_sigma  =       5;
trainingMethod  =       1;
poly_scale      =       [0.8 1 1.2];



partID = 2;
colorset = {'g','g','g','r','b','b','b'};
% 1 --- train deformable model 
%for c =1:cluster_num
    load('weizmann_horse_db/parts/parts_key_points', 'part_key_point');    
    
    clus_members  =     find(exampler_idx(:,partID)); %find(exampler_idx(:,partID)==c);    
    for n = 1:length(clus_members)
        inst_id     =   clus_members(n);
        part_pos(n).point     =   part_key_point{partID}(:,:,inst_id)';  
        fn  =   sprintf('weizmann_horse_db/parts/%d-%03d.png', partID, inst_id);
        part_pos(n).im        =   fn;
    end
   
    part_pos        =   pointtobox(part_pos,par);
    name            =   sprintf('horse_part%d', partID);
    
    boxes           =   [part_pos(1).x1;  part_pos(1).y1; part_pos(1).x2; part_pos(1).y2];
    im              =   imread(part_pos(1).im);
    imshow(im);     hold on; plot(part_pos(1).point(:,1), part_pos(1).point(:,2), 'r*'); hold off;
    showboxes(im, boxes(:)', colorset);
  
    %if ~exist('model', 'var') 
    %    neg = INRIA_data2();
    %    model = trainmodel(name,part_pos,neg, K, par, sbin);   
    %end
    
    cachedir = 'cache/';
    
    numpart = length(par);
    
    % part-dection
    if(0)
    for p = 1:numpart
        cls = [name '_part_' num2str(p) '_mix_' num2str(K(p))];
        load([cachedir cls],'model');
        
        loss = 0;
        for i = 1:length(part_pos)
            im  = imread(part_pos(i).im);
            [box,model,loss] = detect(im, model, model.thresh, [], 0);
            
            showbox     =   box(:,1:4)';
            imshow(im);     hold on; plot(part_pos(1).point(:,1), part_pos(1).point(:,2), 'r*'); hold off;
            %showboxes(im, show_boxes(im,box, colr), colorset);
            show_boxes(im,showbox, 'r');
        end
    end
    end
    
    cls = [name '_final_' num2str(K')'];
    load([cachedir cls],'model');
    for i = [62 102  154 34 81 127 91 44 144 149 128 85 63 155 75 162 55],  %1:length(part_pos)
        im  = imread(part_pos(i).im);
        [box,model,loss] = detect(im, model, model.thresh, [], 0);
        [score ind]    =   max(box(:,end));
        showbox     =   reshape(box(ind,1:end-2)', 4, [])';
        %imshow(im);     hold on; plot(part_pos(1).point(:,1), part_pos(1).point(:,2), 'r*'); hold off;
        %showboxes(im, showbox, colorset);
        showConvell(im, showbox, 'r');
        %show_boxes(im,showbox, 'r');
    end
    
%end



function neg = INRIA_data2()
files = dir('../pose-release1.2-full2/pose-release-ver1.2/code-full/INRIA/*.jpg');
neg = [];
for i = 1:length(files),
  neg(i).im = ['../pose-release1.2-full2/pose-release-ver1.2/code-full/INRIA/' files(i).name];
end     

function show_boxes(im,box, colr)
    imshow(im); hold on;    
    line(box([1 3],:), box([2 4],:), 'color', colr, 'linewidth',2); 
    hold off;
    
function  showConvell(im, box, colr)
    cen_x   =   (box(:,1)+box(:,3))/2;
    cen_y   =   (box(:,2)+box(:,4))/2;
    n   =   size(box,1);
    imshow(im); hold on;    
    line([cen_x cen_x([2:n 1])]', [cen_y cen_y([2:n 1])]', 'color', colr, 'linewidth',2 );
    hold off;
